package com.chen.controller.user;

import com.chen.pojo.entity.User;
import com.chen.pojo.vo.UserVo;
import com.chen.prop.JwtProp;
import com.chen.results.Result;
import com.chen.service.UserService;
import com.chen.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/")
@RequiredArgsConstructor
@Api(tags = "用户登录注销接口管理")
@Slf4j
public class CommonController {
    private final UserService userService;

    private final JwtProp jwtProp;

    @PostMapping("login")
    @ApiOperation(value = "用户登录")
    public Result<UserVo> result(@RequestBody User user) {
        log.info("请求参数有{}", user);
        UserVo login = userService.login(user);
        System.out.println("user = " + login);
        // 生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", login.getId());
        String jwt = JwtUtil.createJWT(jwtProp.getKey(), jwtProp.getTtl(), claims);
        return Result.successLogin(login, jwt);
    }


    // 注销账户
    @DeleteMapping("logout")
    @ApiOperation("注销账户")
    public Result logout() {
        userService.logout();
        return Result.success();
    }




}
